Passed
Push — main ( 1f9a40...3e1577 )
by Stefan
01:51
created

FormGenerator.js ➔ resetElement   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
1
/** global: g_oConfigFromPHP */
2
/** global: FormDataValidator */
3
/** global: jscolor */
4
/** global: CKEDITOR */
5
/** global: FormCKEditor */
6
/** global: RichFmConnector */
7
/** global: FormPicker */
8
9
var g_oCKEdit = null;
10
var g_FDP = null;
11
12
/**
13
 * post load some additional JS scripts after DOM is loaded...
14
 */
15
document.addEventListener('DOMContentLoaded', loadScriptfiles);
16
17
function loadScriptfiles()
18
{
19
    loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormDataValidator.js');
20
    if (g_oConfigFromPHP.Color !== undefined) {
21
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'jscolor.js');
22
    }
23
    
24
    if (g_oConfigFromPHP.DTSel !== undefined) {
25
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'dtsel.js');
26
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormPicker.js');
27
    }
28
29
    if (g_oConfigFromPHP.RichFilemanager !== undefined) {
30
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'RichFmConnector.js');
31
    }
32
    
33
    if (g_oConfigFromPHP.CKEditor !== undefined) {
34
        loadScript(g_oConfigFromPHP.CKEditor.Path + 'ckeditor.js');
35
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormCKEdit.js');
36
    }
37
}
38
39
/**
40
 */
41
42
/**
43
 * Initialization.
44
 * For initialization we lisen to the window onload-event. At this point the 
45
 * additionally loaded script files from the DOMContentLoaded-event should also be 
46
 * available. 
47
 * Dependent on the config from PHP we have to initialize some modules:
48
 * - FormDataValidation needs no initialization
49
 * - if form contains colorpicker(s) we have to initialize them
50
 * - contained date or time pickers also needs initialization
51
 * - embedded CKEditor have to be loaded and configured
52
 */
53
window.addEventListener('load', initFormGenerator);
54
55
function initFormGenerator()
56
{
57
    if (g_oConfigFromPHP.Color !== undefined) {
58
        jscolor.presets.default = g_oConfigFromPHP.Color;
59
        jscolor.init();
60
    }
61
    
62
    if (g_oConfigFromPHP.DTSel !== undefined) {
63
    	g_FDP = new FormPicker(g_oConfigFromPHP);
64
        g_FDP.init();
65
    }
66
    
67
    if (g_oConfigFromPHP.CKEditor !== undefined) {
68
        g_oCKEdit = new FormCKEditor(g_oConfigFromPHP, CKEDITOR);
69
        g_oCKEdit.load();
70
    }
71
}
72
73
/**
74
 * Helper function to create Error/Warning-Message in the document.
75
 * The Messeage is only created, if the debug mode is enabled.
76
 * @param string msg text to display
77
 * @param string level level for the message
78
 */
79
function displayJSError(msg, level)
80
{
81
    if (g_oConfigFromPHP.DebugMode) {
82
        let div = document.createElement('div');
83
        div.id = 'JSError';
84
        let header = document.createElement('h1');
85
        div.appendChild(header);
86
        let body = document.createElement('p');
87
        div.appendChild(body);
88
        header.innerHTML = 'Javascript ' + level;
89
        body.innerHTML = msg;
90
        document.body.insertBefore(div, document.body.firstChild);
91
    }
92
}
93
94
/**
95
 * Validate the form.
96
 */
97
function validateForm()
98
{
99
    var FDV = new FormDataValidator(g_oConfigFromPHP.FormDataValidation);
100
    return FDV.validate();
101
}
102
103
/**
104
 * Call the filemanager to browse for a file on the server.
105
 */
106
function browseServer(editID, imgID, strExpand)
107
{
108
    let FmConnector = new RichFmConnector(g_oConfigFromPHP.RichFilemanager.Path);
109
    FmConnector.editID = editID;
110
    FmConnector.imgID = imgID;
111
    FmConnector.browseServerModal(strExpand);
112
}
113
114
/**
115
 * Handler for the DTU button (Date,Time User)
116
 */
117
function onInsertDateTimeUser(id, strUsername)
118
{
119
    let oEdit = document.getElementById(id);
120
    if (oEdit) {
121
        let date = new Date();
122
        let strValue = oEdit.innerHTML = date.toLocaleString();
123
        if (strUsername != '') {
124
            strValue += ' / ' + strUsername;
125
        }
126
        oEdit.value = strValue;
127
    }
128
}
129
130
/**
131
 * Handler for the reset - Button.
132
 * Used to to reset the content of readonly-inputs or images that get the value
133
 * from filebrowser or another picker etc.
134
 */
135
function resetElement(id)
136
{
137
    let oElement = document.getElementById(id);
138
    if (oElement) {
139
        if (oElement.tagName.toLowerCase() === 'img') {
140
            oElement.src = oElement.getAttribute('data-default');
141
            resetElement(oElement.getAttribute('data-bound-to'));
142
        } else {
143
            oElement.value = '';
144
        }
145
    }
146
}
147
148
/**
149
 * Adjust the height of two columns.
150
 */
151
function adjustColumnHeight(col1, col2)
152
{
153
	var oCol1 = document.getElementById(col1);
154
	var oCol2 = document.getElementById(col2);
155
	if (oCol1 && oCol2) {
156
		if (oCol1.offsetHeight > oCol2.offsetHeight) {
157
			oCol2.style.height = oCol1.offsetHeight + 'px'; 
158
		} else {
159
			oCol1.style.height = oCol2.offsetHeight + 'px'; 
160
		}
161
	}
162
}
163
164
/**
165
 * Dynamic loading of additional scripts.
166
 */
167
function loadScript(strScriptfile)
168
{
169
    var oScript = document.createElement('script');
170
    document.head.appendChild(oScript);
171
    // oScript.onload = function() {console.log('script ' + strScriptfile + ' loaded!');};
172
    oScript.type = 'text/javascript';
173
    oScript.src = strScriptfile;
174
}
175
176